// import axios from 'axios';
export const opaiSDK = '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';

export const chartGPT = (prompt = 'hello world') => {
  // !设置您的API密钥
  const apiKey = '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';
  // 设置API端点
  const apiEndpoint = 'https://api.openai.com/v1/engines/text-davinci-003/completions';
  // 设置请求参数
  // const prompt = 'hello world';
  const maxTokens = 400; // 控制生成文本的长度
  // !设置请求头
  const headers = {
    'Content-Type': 'application/json; charset=UTF-8',
    Authorization: `Bearer ${apiKey}`
  };

  if (typeof axios !== 'undefined') {
    // 发送API请求
    axios
      .post(
        apiEndpoint,
        {
          prompt: prompt,
          max_tokens: maxTokens
        },
        { headers }
      )
      .then(response => {
        const reply = response.data.choices[0].text;
        console.log(reply);
      })
      .catch(error => {
        console.error('Error:', error);
      });
  } else if ('fetch' in window || typeof window.fetch === 'function') {
    fetch(apiEndpoint, {
      method: 'POST',
      headers,
      body: JSON.stringify({
        prompt: prompt,
        max_tokens: maxTokens
      })
    })
      .then(response => response.json())
      .then(data => {
        const reply = data.choices[0].text;
        console.log('reply', reply);
      })
      .catch(error => {
        console.error('Error:', error);
      });
  }
};
